/* indent size: 2 */

module.exports = app => {
  const DataTypes = app.Sequelize;

  const Users = app.model.define('users', {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      comment: "用户id",
    },
    nickName: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "用户昵称",
    },
    avatar: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "用户头像",
    },
    openID: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "微信用户唯一标识",
    },
    userName: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: "用户账号",
    },
    userPassword: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "用户密码",
    },
    type: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: "登录方式【0-账号密码;1-微信一键登录】"
    },
    status: {
      type: DataTypes.INTEGER,
      defaultValue: 1,
      comment: "用户状态【1-正常;0-禁用】",
      allowNull: true,
    },
    remarks: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "用户状态备注",
    },
    totals: {
      type: DataTypes.DECIMAL(11, 2),
      allowNull: true,
      comment: "用户余额",
    },
    token: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "用户登录token",
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: "创建时间",
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: "更新时间",
    }
  }, {
    tableName: 'users'
  });

  Users.associate = function () {
    //一个用户可以拥有多个地址
    app.model.Users.hasMany(app.model.Address, {
      foreignKey: 'id',//定义联结关系中源模型的 key
      targetKey: 'uid',//定义联结关系中目标模型的 key
    });

    //一个用户可以拥有多个订单
    app.model.Users.hasMany(app.model.Orders, {
      foreignKey: 'id',//定义联结关系中源模型的 key
      targetKey: 'uid',//定义联结关系中目标模型的 key
    });

    //一个用户可以拥有一个购物车
    app.model.Users.hasOne(app.model.Shoppingcarts, {
      foreignKey: 'id',//定义联结关系中源模型的 key
      targetKey: 'uid',//定义联结关系中目标模型的 key
    });


  }

  return Users;
};
